import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: '/about',
    children: [
      {
        path: 'about',
        name: 'About',
        component: () => import('../views/About'),
        meta: {
          title: '首页'
        }
      }, {
        path: 'filmInfo',
        name: 'FilmInfo',
        component: () => import('../views/film/filmList/FilmInfo'),
        meta: {
          title: '用户列表',
          parentTitle: '电影管理'
        }
      }, {
        path: 'filmType',
        name: 'FilmType',
        component: () => import('../views/film/movieType/FilmType'),
        meta: {
          title: '电影类型',
          parentTitle: '电影管理'
        }
      }, {
        path: 'filmReview',
        name: 'FilmReview',
        component: () => import('../views/film/comment/FilmReview'),
        meta: {
          title: '影评管理',
          parentTitle: '电影管理'
        }
      }, {
        path: 'actor',
        name: 'Actor',
        component: () => import('../views/film/actor/Actor'),
        meta: {
          title: '演员管理',
          parentTitle: '电影管理'
        }
      }, {
        path: 'filmEdit',
        name: 'FilmEdit',
        component: () => import('../views/film/filmList/FilmEdit')
      },
      // 新闻列表
      {
        name: 'News',
        path: '/news',
        component: () => import('../views/news/News.vue')
      },
      {
        name: 'NewsEdit',
        path: '/newsEdit',
        component: () => import('../views/news/NewsEdit.vue')
      },
      //  新闻类型
      {
        name: 'NewsType',
        path: '/newsType',
        component: () => import('../views/news/newsType/NewsType.vue'),
        meta: {
          title: '新闻类型',
          parentTitle: '新闻管理'
        }
      },
      {
        name: 'NewsTypeEdit',
        path: '/NewsTypeEdit',
        component: () => import('../views/news/newsType/NewsTypeEdit')
      },
      //  会员列表
      {
        name: 'Vip',
        path: '/vip',
        component: () => import('../views/vip/Vip.vue')
      },
      //  会员详情
      {
        name: 'VipDetails',
        path: '/vipDetails',
        component: () => import('../views/vip/collect/VipInfo.vue')
      },
      // 广告路由
      {
        name: 'Ad',
        path: '/ad',
        component: () => import('../views/ad/Ad.vue')
      },
      // 广告编辑
      {
        name: 'AdEdit',
        path: '/adEdit',
        component: () => import('../views/ad/AdEdit.vue')
      },
      {
        name: 'TypeEdit',
        path: '/TypeEdit',
        component: () => import('../views/film/movieType/TypeEdit')
      },
      {
        name: 'ActorEdit',
        path: '/ActorEdit',
        component: () => import('../views/film/actor/ActorEdit')
      }, {
        name: 'ActorSearch',
        path: '/ActorSearch',
        component: () => import('../views/film/actor/ActorSearch')
      }, {
        name: 'User',
        path: '/User',
        component: () => import('../views/user/User')
      }
    ]
  },
  {
    name: 'Login',
    path: '/login',
    component: Login
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  router.app.$options.store.state.defaultActive = to.path.substr(1)
  if (to.path === '/login') {
    next()
  } else {
    if (Vue.$cookies.isKey('token')) {
      next()
    } else {
      next({ path: '/login' })
    }
  }
})

const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

export default router
